package main.java.org.service.serviceImpl;

import main.java.Exam.pojo.Entity.Log;
import main.java.Exam.pojo.Entity.Operatorlog;
import main.java.Exam.pojo.Entity.PageResult;
import main.java.Exam.pojo.Entity.User;
import main.java.Exam.pojo.tableDTO.PageDto;
import main.java.Exam.pojo.tableVO.OperatorlogVo;
import main.java.Exam.pojo.tableVO.adminVo;
import main.java.org.mapper.AdminMapper;
import main.java.org.mapper.LogMapper;
import main.java.org.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class AdminServiceimpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

@Autowired
    private LogMapper logMapper;
    private String student="学生管理";
    private String teacher="教师管理";
    @Override
    public PageResult getTotalStudent(PageDto pageDto) {

        int roleId = 1;
        int page=(pageDto.getPage()-1)*pageDto.getPageSize();
        List<User> list = adminMapper.getPageStudent(roleId,page, pageDto.getPageSize());
        int total = adminMapper.getTotalStudent(roleId);

        return new PageResult(total, list);
    }

    @Override
    public PageResult getTotalTeacher(PageDto pageDto) {
        int roleId = 2;
        int page=(pageDto.getPage()-1)*pageDto.getPageSize();
        List<User> list = adminMapper.getPageStudent(roleId,page, pageDto.getPageSize());
        int total = adminMapper.getTotalStudent(roleId);
        return new PageResult(total, list);
    }

    @Override
    public void insertData(adminVo adminVo) {
        User user=new User();
        user.setUserAccount(adminVo.getUserAccount());
        user.setRoleId(adminVo.getRoleID());
        user.setPhone(adminVo.getPhone());
        user.setUserName(adminVo.getUserName());
        adminMapper.insertData(user);
    }

    @Override
    public void updateData(String userAccount, Integer roleId) {
     //   logexam(userAccount,"陈J","添加管理员","管理员");
        adminMapper.updata(userAccount,roleId);
    }

    @Override
    public void deleteData(String userAccount) {

   //     logexam(userAccount,"陈J","删除用户","用户");
        adminMapper.deleteData(userAccount);
    }

    @Override
    public User getSingleData(String userAccount) {
      //  logexam(userAccount,"陈J","查询用户","用户");
        User user=adminMapper.selectData(userAccount);
        return user;
    }

    @Override
    public PageResult getLog(PageDto pageDto) {
        int page=(pageDto.getPage()-1)*pageDto.getPageSize();
        List<Operatorlog> list = adminMapper.getLog(page, pageDto.getPageSize());
        List<OperatorlogVo>voList=new ArrayList<>();
        for (Operatorlog operatorlog : list) {
            OperatorlogVo operatorlogVo=new OperatorlogVo();
            operatorlogVo.setContent(operatorlog.getContent());
            operatorlogVo.setModule(operatorlog.getModule());
            operatorlogVo.setOperationType(operatorlog.getOperationType());
            operatorlogVo.setOperationTime(operatorlog.getOperationTime());
            operatorlogVo.setOperatorAccount(operatorlog.getOperatorAccount());
            operatorlogVo.setErrorMsg(null);
            operatorlogVo.setId(operatorlog.getId());
            if(operatorlog.getResult()==1)
            {
                operatorlogVo.setResult("true");
            }
            operatorlogVo.setOperatorName(operatorlog.getOperatorName());
            //添加进去
            voList.add(operatorlogVo);
        }
        int total = adminMapper.getLogCount();
        //logexam("12312345","陈J","管理员查询日志","管理员");
        return new PageResult(total, voList);

    }
/*    public Log logexam(String operatorAccount, String operatorName, String operationType, String module){
        Log log1=new Log();
        log1.setOperatorAccount(operatorAccount);
        log1.setOperatorName(operatorName);
        log1.setOperationTime(LocalDateTime.now());
        log1.setResult("1");
        log1.setModule(module);
        log1.setOperationType(operationType);

        logMapper.inset(log1);
        return log1;
    }*/

}
